//
// Created by 27819 on 24-11-28.
//

#include <iostream>
#include <linux/videodev2.h>

#include "../v4l2_video/v4l2_video.h"

void dump_image(const std::string& image_data)
{
    static unsigned long long index = 0;
    std::string file_name = "/home/licks/test_v4l2/img_" + std::to_string(index) + ".jpg";
    FILE* fp = fopen(file_name.c_str(), "wb");
    if (nullptr != fp)
    {
        fwrite(image_data.c_str(), image_data.size(), 1, fp);
    }
    fclose(fp);
    index++;
}

int main(int argc, char **argv)
{
    CV4L2Video video("/dev/video0");
    if (!video.open_video())
    {
        std::cerr << "Open video failed" << std::endl;
        return -1;
    }
    //判断是否是视频采集设备
    if (!video.is_capture())
    {
        return -2;
    }
    //设置视频参数
    if (bool ret = video.set_video_parameter(V4L2_PIX_FMT_MJPEG, Resolution(640, 480), 25))
    {
        std::cout << "success" << std::endl;
    }
    else
    {
        std::cout << "failer" << std::endl;
    }
    //申请内存
    video.v4l2_malloc_buffer();
    //开始采集
    if (!video.start_capture())
    {
        return -3;
    }
    std::string image;
    video.get_frame_image(image, dump_image);
    //停止采集
    if (!video.stop_capture())
    {
        return -4;
    }

    return 0;
}
